import 'dart:async';

class RelayStreamController<T> {
  final StreamController<T> _controller = StreamController.broadcast();

  T _value;

  RelayStreamController({required T value}) : _value = value;

  T get value => _value;

  Stream<T> get stream => _controller.stream;

  void add(T value) {
    _value = value;
    _controller.sink.add(value);
  }

  StreamSubscription<T> listen(void Function(T event) onData,
      {Function? onError, void onDone()?, bool? cancelOnError}) {
    onData(_value);
    return _controller.stream.listen(onData,
        onError: onError, onDone: onDone, cancelOnError: cancelOnError);
  }
}
